
<?php
	include("DBConn.php");
	$item_name = $_POST['item_name'];
	$quantity = $_POST['quantity'];
	$category = $_POST['category'];
	$source = $_POST['source'];
?>


<html>
<body>

<?php
	$link = connectToDB();
	$rowQuery = "select * from new_items where ITEM_NAME='$item_name'";
	$resultrow = mysql_query($rowQuery);
	
	$strQuery = "insert into new_items values ('$item_name', '$quantity', '$category', '$source', CURRENT_TIMESTAMP);";
	$result = mysql_query($strQuery);
	$strQuery = "insert into added_items values ('$item_name', '$quantity', CURRENT_TIMESTAMP);";
	$result = mysql_query($strQuery);
	$strQuery0 = "insert into item_status values ('$item_name', '$quantity', '', '$category', '', '', CURRENT_TIMESTAMP);";
	$result0 = mysql_query($strQuery0);
	
	$strQuery1 = "select * from new_items where CATEGORY='$category' order by DATE_ADDED desc";
	$result1 = mysql_query($strQuery1);
	
	
	echo "<center>";
	$rowCount=mysql_num_rows($resultrow); 
	if($rowCount==1)
		echo "<h1>Failed to add new item!<br/>Item is already in the inventory.</h1>";
	else {
		echo "<h1>Successfully added new item!</h1>";
	}
	echo "</center>"
?>
	<center>
	<table border="1" cellspacing="0" width="80%">
		<tr>
		<?php
		while ($field=mysql_fetch_field($result1)) {
		echo "<th>";
		echo "<center>$field->name</center>";
		echo "</th>";
		}
		echo "</tr>";

		while ($row = mysql_fetch_row($result1)) {
			if($row[0]=="$item_name") echo "<tr style='background-color: lightgray;'>";
			else echo "<tr>";
			for ($i=0; $i<mysql_num_fields($result1); $i++) {
				echo "<td>";
				echo "<center>$row[$i]</center>";
				echo "</td>";
			}
			echo "</tr>\n";
		}

	echo "</table></center>";
	mysql_close($link);
?>

</body>
</html>